//
//  ZYCProfileUserInfomationViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/20.
//

#import "ZYCProfileUserInfomationViewController.h"
#import "ZYCProfileUserInfomationCell.h"
#import "ZYCProfileUserHeaderInfoCell.h"
#import "ZYCProfileModifyUserInfomationViewController.h"
#import "ZYCProfileUserInfoFooterView.h"
#import "ZYCUserLoginModel.h"
#import "ZYCProfileFaceEntyAlbumViewController.h"
#import "SystemCamera.h"
#import "ZYCConsummateViewController.h"
@interface ZYCProfileUserInfomationViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCProfileUserInfoFooterViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) UIButton *moreButton;
@property (nonatomic, copy) NSString *realName;
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, copy) NSString *phoneNumber;
@property (nonatomic, copy) NSString *role;
@property (nonatomic, copy) NSString *roleType;
@property (nonatomic, copy) NSString *customizePath;

@end

@implementation ZYCProfileUserInfomationViewController
#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人信息";
    [self.view addSubview:self.tableView];



}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    NSInteger roleType =  [[NSUserDefaults standardUserDefaults] integerForKey:@"kUserRoleType"];
    if (roleType == 0) {
        self.roleType = @"1";
        self.role = @"教师";
    }else{
        self.roleType = @"2";
        self.role = @"家长";
    }
    [self getUserInfo];
}


- (void)getUserInfo{
    NSDictionary *params = @{
        @"roleType":self.roleType
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Profile_Get_GetMyInfo requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            [ZYCUserLoginModel deleteUserLogin];
            ZYCUserLoginModel *userLogin = [ZYCUserLoginModel yy_modelWithDictionary:responseObject[@"body"]];
            [userLogin saveUserLogin];
            self.realName = userLogin.realName;
            self.userName = userLogin.userName;
            self.phoneNumber = userLogin.phonenumber;
            self.customizePath = userLogin.customizePath;
            [self.tableView reloadData];
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ZYCProfileUserHeaderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCProfileUserHeaderInfoCell class]) forIndexPath:indexPath];
        cell.titleString = self.dataSource[indexPath.section];
        cell.avaterInfo = self.customizePath;
        return cell;
    }else{
        ZYCProfileUserInfomationCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCProfileUserInfomationCell class]) forIndexPath:indexPath];
        cell.titleString = self.dataSource[indexPath.section];
        if (indexPath.section == 1) {
            cell.contentString = self.realName;
        }else  if (indexPath.section == 2) {
            cell.contentString = self.userName;
        }else  if (indexPath.section == 3) {
            cell.contentString = self.phoneNumber;
        }else  if (indexPath.section == 4) {
            cell.contentString = self.role;
        }
        return cell;
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 17.0f;
    }
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 4) {
        return 300.0f;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    // 完善更多资料
    if (section == 4) {
        ZYCProfileUserInfoFooterView *footerView = [[ZYCProfileUserInfoFooterView alloc] init];
        footerView.titleString = @"完善更多资料";
        footerView.delegate = self;
        return footerView;;
    }
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
   NSString *title = self.dataSource[indexPath.section];
    if (indexPath.section == 0) {
        [self clickHeader];
    }else if (indexPath.section == 1 || indexPath.section == 3) {
        ZYCProfileModifyUserInfomationViewController *vc = [ZYCProfileModifyUserInfomationViewController new];
        NSString *titleString = [NSString stringWithFormat:@"修改%@",title];
        NSString *str  = [titleString componentsSeparatedByString:@":"].firstObject;
        vc.titleString = str;
        if ([title isEqualToString:@"姓名:"]) {
            vc.isShowTip = YES;
            vc.placeHolder = @"请输入您的姓名";
            vc.type = ZYCModifyUserInfoViewUserName;
        }else if ([title isEqualToString:@"联系方式:"]) {
            vc.isShowTip = NO;
            vc.placeHolder = @"请输入您的联系方式";
            vc.type = ZYCModifyUserInfoViewPhone;
        }
        if (indexPath.section == 1) {
            vc.modifyContent = self.realName;
        }else if(indexPath.section == 3){
            vc.modifyContent = self.phoneNumber;
        }

        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (void)clickHeader{
    ZYCWeakSelf
    [SystemCamera sharedSystemCamera].SelectImage = ^(UIImage *headerImage, NSInteger buttonIndex) {
        ZYCProfileFaceEntyAlbumViewController *vc = [[ZYCProfileFaceEntyAlbumViewController alloc] init];
        vc.headerImage = headerImage;
        if (buttonIndex == 0) {
            vc.type = ZYCProfileFaceEntryViewTypePhoto;
        }else{
            vc.type = ZYCProfileFaceEntryViewTypeAlbum;
        }
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };

    [SystemCamera cameraWillShowInController:self];
}


#pragma mark - ZYCProfileUserInfoFooterViewDelegate
- (void)profileUserInfoFooterViewDidMore{
    [MrjToast makeToast:@"正在努力研发中..." makeView:self.view];
//    ZYCConsummateViewController *vc = [[ZYCConsummateViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
}
- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource =  @[@"人脸:",@"姓名:",@"账号:",@"联系方式:",@"账号身份:"];
    }
    return _dataSource;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight ) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCProfileUserInfomationCell class] forCellReuseIdentifier:NSStringFromClass([ZYCProfileUserInfomationCell class])];
        [_tableView registerClass:[ZYCProfileUserHeaderInfoCell class] forCellReuseIdentifier:NSStringFromClass([ZYCProfileUserHeaderInfoCell class])];
        
    }
    return _tableView;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
